<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Pre_define_entity{
	var $id = 0;
	var $parent_id = 0;
	var $is_cat = "";
	var $is_event = "";
	var $title = "";
	var $odds = 0;
	var $sport_id = 0;
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_PRE_DEFINE)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->parent_id = $result[0]->parent_id;
			$this->is_cat = $result[0]->is_cat;
			$this->is_event = $result[0]->is_event;
			$this->title = $result[0]->title;
			$this->odds = $result[0]->odds;
			$this->sport_id = $result[0]->sport_id;
			
			return $this;
		}
		return false;
	}
	
	function setParentId($input){ $this->parent_id=$input; }
	function setIsCat($input){ $this->is_cat=$input; }
	function setIsEvent($input){ $this->is_event=$input; }
	function setTitle($input){ $this->title=$input; }
	function setOdds($input){ $this->odds=$input; }
	function setSportId($input){ $this->sport_id=$input; }
	
	function getId(){ return $this->id; }
	function getParentId(){ return $this->parent_id; }
	function getIsCat(){ return $this->is_cat; }
	function getIsEvent(){ return $this->is_event; }
	function getTitle(){ return $this->title; }
	function getOdds(){ return $this->odds; }
	function getSportId(){ return $this->sport_id; }
	
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('parent_id', $this->parent_id);
		$ci->db->set('is_cat', $this->is_cat);
		$ci->db->set('is_event', $this->is_event);
		$ci->db->set('title', $this->title);
		$ci->db->set('odds', $this->odds);
		$ci->db->set('sport_id', $this->sport_id);
		
		$ci->db->where('id', $this->id)->update(TBL_PRE_DEFINE);
	}
	
	function createNewEntity(){
		$ci=& get_instance();
		$ci->db->set('parent_id', NULL);
		$ci->db->insert(TBL_PRE_DEFINE);
		return $ci->db->insert_id();
	}
	
	function getListCategory(){
		$ci=& get_instance();
		return $ci->db->where('is_cat','yes')->get(TBL_PRE_DEFINE)->result();
	}
	
	function getListCategoriesBySportId($sport_id){
		$ci=& get_instance();
		return $ci->db->where(array('is_cat'=>'yes','sport_id'=>$sport_id))->get(TBL_PRE_DEFINE)->result();
	}
	
	function getListEventOfCategory($cat_id){
		$ci=& get_instance();
		return $ci->db->where( array('is_event'=>'yes','parent_id'=>$cat_id) )->get(TBL_PRE_DEFINE)->result();
	}
	
}
